<?php
/**
 * 服务窗应用激活开发者模式--应用网关
 * User: zym(wika)
 * Date: 2016/12/1
 * Time: 11:41
 */
class AlipayGatewayTAction extends CommonAction
{
    public $configArr;

    public function _initialize()
    {
        $this->configArr = array(
            'alipay_public_key' => "/mnt/lost+found/www/wap.alpha.greencampus.cc/uploads/alipay/rsa/common/alipay_rsa2_public_key.pem",
            'merchant_private_key' => "/mnt/lost+found/www/wap.alpha.greencampus.cc/uploads/alipay/rsa/common/rsa2_private_key.pem",
            'merchant_public_key' => "/mnt/lost+found/www/wap.alpha.greencampus.cc/uploads/alipay/rsa/common/rsa2_public_key.pem",
            /*'alipay_public_key_file' => "/mnt/lost+found/www/pos_pay.xiaotui.so/upload/alipay/alipay_rsa_public_key.pem",
            'merchant_private_key_file' => "/mnt/lost+found/www/pos_pay.xiaotui.so/upload/alipay/rsa_private_key.pem",
            'merchant_public_key_file' => "/mnt/lost+found/www/pos_pay.xiaotui.so/upload/alipay/rsa_public_key.pem",*/
            'sign_type' => "RSA2",
            'charset' => "GBK",
            'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
            'app_id' => "2017042406934384"
        );
    }

    /**
     * 此文件未对接支付宝服务器的网关文件，将此文件的访问路径填入支付宝服务窗的开发中验证网关的页面中。
     * 次文件接收支付宝服务器发送的请求
     */
    public function index()
    {
        if (get_magic_quotes_gpc()) {
            foreach ($_POST as $key => $value) {
                $_POST [$key] = stripslashes($value);
            }

            foreach ($_GET as $key => $value) {
                $_GET [$key] = stripslashes($value);
            }
            foreach ($_REQUEST as $key => $value) {
                $_REQUEST [$key] = stripslashes($value);
            }
        }
        wLog( "POST: " . var_export($_POST,true),'getway');
        ksort($_POST);
        $params = $_POST;
        $sign = $params ['sign'];
        unset($params['sign']);
        /* 返回验签结果 */
        wLog( "params: " . var_export($params,true),'getway');
        $stringToBeSigned = "";
        $i = 0;
        foreach ($params as $k => $v) {
            if ($i == 0) {
                $stringToBeSigned .= "$k" . "=" . "$v";
            } else {
                $stringToBeSigned .= "&" . "$k" . "=" . "$v";
            }
            $i++;
        }
        unset($k, $v);
        wLog( "stringToBeSigned: " . $stringToBeSigned,'getway');
        //$rsaPublicKeyFilePath = "/mnt/lost+found/www/wap.alpha.greencampus.cc/uploads/alipay/rsa/common/alipay_rsa2_public_key.pem";
        /* 读取公钥文件，PEM格式 */
        //$pubKey = file_get_contents($rsaPublicKeyFilePath);
        $pubKey="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhpkWXu34S8xRH1lJeVnWBq5p1B0T3l0xri8RvqRJ46e/RJMebD4/3bVaqyc4Z7hbFafn6XglqU2kGdA9INHX70QrV/Pp+cOJLo5EPApc4N5t61cTzk+eQ5D0jHJK86O371xcrbD9dsKAyOH4HMQi//9UFqEjJy20c8LhnwR3qzAfyc5l0CKbJi598qBhwL1ID6sphEaiayaIFmhBeuKkxq2ddDJesCP1BGnOFH/9lIH65f/rKcBrR6kbNUBiMl8IvO6PLy9/hzJpQvWDNAhR2xTVKIzWQgYe8uizh/NjTOjO2blVnEGpwl/o5J7I+LLdt8uN6yZeMJggiSFnr0ox5QIDAQAB";
        $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
            wordwrap($pubKey, 64, "\n", true) .
            "\n-----END RSA PRIVATE KEY-----";
        wLog( "pubKey: " . $pubKey,'getway');
        /* 转换为openssl格式密钥 */
        //$res = openssl_get_publickey($pubKey);
        wLog( "res: " . $res,'getway');
        /* 调用openssl内置方法验签 */
        $result = (bool) openssl_verify($stringToBeSigned, base64_decode($sign), $res,OPENSSL_ALGO_SHA256);
        /* 释放资源 */
        openssl_free_key($res);
        /* 返回验签结果 */
        wLog( "result: " . serialize($result),'getway');
    }


    public function verifygw($is_sign_success, $as)
    {
        $biz_content = $this->_request("biz_content");
        wLog ( "biz_content:".var_export($biz_content),'getway' );
        $xml = simplexml_load_string($biz_content);
        // print_r($xml);
        $EventType = ( string )$xml->EventType;
        // echo $EventType;
        if ($EventType == "verifygw") {
            $as->rsaPrivateKey = $this->configArr['merchant_private_key'];
            // global $this->configArr;
            // print_r ( $this->configArr );
            if ($is_sign_success) {
                $response_xml = "<success>true</success><biz_content>" . $this->configArr ['merchant_public_key'] . "</biz_content>";

            } else { // echo $response_xml;
                $response_xml = "<success>false</success><error_code>VERIFY_FAILED</error_code><biz_content>" . $this->configArr ['merchant_public_key_file'] . "</biz_content>";
            }

            $mysign = $as->alonersaSign($response_xml, $this->configArr['merchant_private_key'], $this->configArr['sign_type']);
            $return_xml = "<?xml version=\"1.0\" encoding=\"" . $this->configArr['charset'] . "\"?><alipay><response>" . $response_xml . "</response><sign>" . $mysign . "</sign><sign_type>" . $this->configArr['sign_type'] . "</sign_type></alipay>";
            wLog ( "return_xml:".$return_xml,'getway' );
            echo $return_xml;
            exit ();
        }
    }
}

